import { useSelector,useDispatch } from "react-redux";
import { useCallback } from "react";
import usersApi from "../api/users.js";
// import * as Creators from "../store/actions";
import Creators from "../store/actions.js";

export const useUsers = () =>{

    const users = useSelector(state =>state.users);
    const dispatch = useDispatch();
    const {curPage,eachPage} = users;

    // console.log(curPage,eachPage);

    const getUsers = useCallback( async()=>{
        
        const data = await usersApi.get({curPage,eachPage});
        dispatch(Creators.getUsers(data));
    },[curPage,eachPage,dispatch]);
    
    const setCurPageUsers = useCallback((curPage)=>{
        
        dispatch(Creators.setCurPage({curPage}))
    },[dispatch]);

    const setEachPageUsers = useCallback((eachPage)=>{
        users.rows=[];
        dispatch(Creators.setEachPage({eachPage}))
    },[dispatch]);
 
    return [users,{getUsers,setCurPageUsers,setEachPageUsers}]
}